package com.test.ws.s1.service.imp;

import com.test.ws.s1.bean.Acl_permission;
import com.test.ws.s1.bean.Acl_role_permission;
import com.test.ws.s1.mapper.Acl_permissionDao;
import com.test.ws.s1.service.Acl_permissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class Acl_permissionServiceImp implements Acl_permissionService {
    @Autowired
    private Acl_permissionDao acl_permissionDao;

    @Override
    public List<Acl_permission> selectAcl_permission(Acl_permission a) {
        // 获取所有权限
        List<Acl_permission> aclPermission = acl_permissionDao.selectAcl_permission(a);
        // 改造
        List<Acl_permission> newList = refactor(aclPermission);
        return newList;
    }

    public List<Acl_permission> refactor(List<Acl_permission> list){
        List<Acl_permission> newList = new ArrayList<>();
        // 获取父级节点放入集合
        list.stream().forEach(acl_permission -> {
            if(acl_permission.getPid() == 0){
                List<Acl_permission> collect = list.stream().filter(ap -> ap.getPid() == acl_permission.getPerm_id()).collect(Collectors.toList());
                acl_permission.setChildren(collect);
                newList.add(acl_permission);
            }
        });
        return newList;
    }

    @Override
    public List<Acl_permission> selectAcl_permissionByRoleId(Integer role_id) {
        return acl_permissionDao.selectAcl_permissionByRoleId(role_id);
    }

    @Override
    public int deleteAcl_permission(Integer id) {
        return acl_permissionDao.deleteAcl_permission(id);
    }

    @Override
    public int insertAcl_permission(Acl_role_permission a) {
        return acl_permissionDao.insertAcl_permission(a);
    }
}
